package com.joinus.server.dao;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.joinus.server.constant.Constant;
import com.joinus.server.entity.MeetingUsers;
import com.joinus.server.tools.Tools;

/**
 * 用户与活动关系数据库操作层<br>
 * @author
 * <ul> 
 * <li><a href="mailto:qipenghui3056@gmail.com">henrybit</a></li>
 * </ul>
 * @version 1.0
 * @since 2013-4-4
 */
public class MeetingUsersDao extends BaseDao {
	/**活动与参会者关系Dao实例*/
	private final static MeetingUsersDao meetingUsersDao = new MeetingUsersDao();
	/**插入SQL前缀*/
	private final static String INSERT_NEW_MEETING_USERS = "insert into meeting_users (id,meeting_id,create_user_id,join_users_id, create_date, update_date) values";
	/**查询SQL前缀*/
	private final static String QUERY_MEETING_USERS = "select * from meeting_users ";
	private MeetingUsersDao(){
	}
	
	/**
	 * 获取MeetingUsersDao实例<br>
	 * @return meetingUsersDao
	 */
	public static MeetingUsersDao getInstance() {
		return meetingUsersDao;
	}
	
	/**
	 * 返回有指定人员参与的活动列表<br>
	 * @param uid 参与活动人员ID
	 * @param updateTime 更新时间
	 * @return List-活动列表
	 */
	public List<MeetingUsers>  getMeetingUsersList(String uid, Date updateTime) {
		List<MeetingUsers> meetingList = new ArrayList<MeetingUsers>();
		
		StringBuilder sql = new StringBuilder(QUERY_MEETING_USERS);
		sql.append(" where join_users_id like '%").append(uid).append("%'");;
		sql.append(" and update_date>='").append(Tools.getDateFormat(updateTime, Constant.DATE_YYYY_MM_DD_HH_MM_SS)).append("'");
		
		ResultSet rs = super.query(sql.toString());
		
		try {
			while(rs.next()) {
				long meetingId = rs.getInt("meeting_id");
				String createUserId = rs.getString("create_user_id");
				String joinUsersId = rs.getString("join_users_id");
				
				MeetingUsers meetingUsers = new MeetingUsers();
				meetingUsers.setMeetingId(meetingId);
				meetingUsers.setCreateUserId(createUserId);
				meetingUsers.setJoinUsersId(joinUsersId);
				
				meetingList.add(meetingUsers);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return meetingList;
	}
	
	/**
	 * 返回指定活动的参与信息<br>
	 * @param uid 创建活动者ID
	 * @param meetingId 活动ID
	 * @return List-活动参与人员相关信息
	 */
	public List<MeetingUsers> getMeetingUsersList(String uid, long meetingId) {
		List<MeetingUsers> meetingList = new ArrayList<MeetingUsers>();
		
		StringBuilder sql = new StringBuilder(QUERY_MEETING_USERS);
		sql.append(" where create_user_id='").append(uid).append("'");;
		sql.append(" and meeting_id=").append(meetingId);
		
		ResultSet rs = super.query(sql.toString());
		try {
			while(rs.next()) {
				meetingId = rs.getInt("meeting_id");
				String createUserId = rs.getString("create_user_id");
				String joinUsersId = rs.getString("join_users_id");
				
				MeetingUsers meetingUsers = new MeetingUsers();
				meetingUsers.setMeetingId(meetingId);
				meetingUsers.setCreateUserId(createUserId);
				meetingUsers.setJoinUsersId(joinUsersId);
				
				meetingList.add(meetingUsers);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return meetingList;
	}
	
	/**
	 * 创建一个活动与参与者列表关系<br>
	 * @param meetingId 活动ID
	 * @param createUserId 创建者ID
	 * @param joinUsersId 参与者ID列表
	 * @return long-创建纪录成功的主键ID，失败返回－1.
	 */
	public long createNewMeetingUsers(long meetingId, String createUserId, String joinUsersId) {
		return this.createNewMeetingUsers(meetingId, createUserId, joinUsersId, null, null);
	}
	
	/**
	 * 创建一个活动与参与者列表关系<br>
	 * @param meetingId 活动ID
	 * @param createUserId 创建者ID
	 * @param joinUsersId 参与者ID列表
	 * @param createTime 创建时间
	 * @param updateTime 更新时间
	 * @return long-创建纪录成功的主键ID，失败返回－1.
	 */
	public long createNewMeetingUsers(long meetingId, String createUserId, String joinUsersId, String createTime, String updateTime) {
		StringBuilder sql = new StringBuilder(INSERT_NEW_MEETING_USERS);
		//id auto increatement by db
		sql.append("(null");
		
		//meetingId
		sql.append(",").append(meetingId);
		
		//createUserId
		sql.append(",'").append(createUserId).append("'");
		
		//joinUsersId
		sql.append(",'").append(joinUsersId).append("'");
		
		//createTime and updateTime
		Date now = new Date();
		if(createTime == null) {
			createTime = Tools.getDateFormat(now, Constant.DATE_YYYY_MM_DD_HH_MM_SS);
		}
		if(updateTime == null) {
			updateTime = Tools.getDateFormat(now, Constant.DATE_YYYY_MM_DD_HH_MM_SS);
		}
		sql.append(",'").append(createTime).append("'");
		sql.append(",'").append(updateTime).append("'");
		
		sql.append(")");
		
		ResultSet rs = super.insert(sql.toString());
		long id = -1L;
		try {
			rs.next();
			id = rs.getLong(1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}
}
